// 获取ios版本号
function get_ios_version() {
  var ua = navigator.userAgent.toLowerCase();
  var version = null;
  if (ua.indexOf("like mac os x") > 0) {
    var reg = /os [\d._]+/gi;
    var v_info = ua.match(reg);
    version = (v_info + "").replace(/[^0-9|_.]/ig, "").replace(/_/ig, "."); //得到版本号9.3.2或者9.0
    version = parseInt(version.split('.')[0]) + '.' + parseInt(version.split('.')[1]); // 得到版本号第一位
  }
  return version;
}

// 获取android版本号
function get_android_version() {
  var ua = navigator.userAgent.toLowerCase();
  var version = null;
  if (ua.indexOf("android") > 0) {
    var reg = /android [\d._]+/gi;
    var v_info = ua.match(reg);
    version = (v_info + "").replace(/[^0-9|_.]/ig, "").replace(/_/ig, "."); //得到版本号4.2.2
    version = parseInt(version.split('.')[0]) + '.' + parseInt(version.split('.')[1]);// 得到版本号第一位
  }
  return version;
}

// 获取操作系统信息
function get_os_info() {
  let sUserAgent = navigator.userAgent;
  let isWin = (navigator.platform == "Win32") || (navigator.platform == "Windows");
  let isMac = (navigator.platform == "Mac68K") || (navigator.platform == "MacPPC") || (navigator.platform == "Macintosh") || (navigator.platform == "MacIntel");
  let isiPhone = navigator.userAgent.indexOf('iPhone') > -1;
  if (isiPhone) return "iOS " + get_ios_version()
  if (isMac) return "Mac";
  let isUnix = (navigator.platform == "X11") && !isWin && !isMac;
  if (isUnix) return "Unix";
  let isAndroid = navigator.userAgent.indexOf('Android') > -1;
  if (isAndroid) return "Android " + get_android_version();
  let isLinux = (String(navigator.platform).indexOf("Linux") > -1);
  if (isLinux) return "Linux";
  if (isWin) {
    let isWin2K = sUserAgent.indexOf("Windows NT 5.0") > -1 || sUserAgent.indexOf("Windows 2000") > -1;
    if (isWin2K) return "Win2000";
    let isWinXP = sUserAgent.indexOf("Windows NT 5.1") > -1 || sUserAgent.indexOf("Windows XP") > -1;
    if (isWinXP) return "WinXP";
    let isWin2003 = sUserAgent.indexOf("Windows NT 5.2") > -1 || sUserAgent.indexOf("Windows 2003") > -1;
    if (isWin2003) return "Win2003";
    let isWinVista = sUserAgent.indexOf("Windows NT 6.0") > -1 || sUserAgent.indexOf("Windows Vista") > -1;
    if (isWinVista) return "WinVista";
    let isWin7 = sUserAgent.indexOf("Windows NT 6.1") > -1 || sUserAgent.indexOf("Windows 7") > -1;
    if (isWin7) return "Win7";
    let isWin8 = sUserAgent.indexOf("windows nt 6.2") > -1 || sUserAgent.indexOf("windows 8") > -1;
    if (isWin8) return "Win8";
    let isWin81 = sUserAgent.indexOf("windows nt 6.3") > -1;
    if (isWin81) return "Win8.1";
    let isWin10 = sUserAgent.indexOf("Windows NT 10") > -1 || sUserAgent.indexOf("Windows 10") > -1;
    if (isWin10) return "Win10";
  }
  return "Unknown";
}

// 获取浏览器信息
function get_browse_info() {
  let browser = { ie: '', firefox: '', chrome: '', opera: '', safari: '' };
  let userAgent = navigator.userAgent.toLowerCase();
  let s;
  (s = userAgent.match(/msie ([\d.]+)/)) ? browser.ie = s[1] : (s = userAgent.match(/firefox\/([\d.]+)/)) ? browser.firefox = s[1] : (s = userAgent.match(/chrome\/([\d.]+)/)) ? browser.chrome = s[1] : (s = userAgent.match(/opera.([\d.]+)/)) ? browser.opera = s[1] : (s = userAgent.match(/version\/([\d.]+).*safari/)) ? browser.safari = s[1] : 0;
  let version = "";
  if (browser.ie) {
    version = 'IE ' + browser.ie;
  }
  else {
    if (browser.firefox) {
      version = 'firefox ' + browser.firefox;
    }
    else {
      if (browser.chrome) {
        version = 'chrome ' + browser.chrome;
      }
      else {
        if (browser.opera) {
          version = 'opera ' + browser.opera;
        }
        else {
          if (browser.safari) {
            version = 'safari ' + browser.safari;
          }
          else {
            version = 'Unknown';
          }
        }
      }
    }
  }
  return version;
}

// 获取当前地址
function get_base_url() {
  return window.location.protocol + '//' + window.location.host
}

//移动终端浏览userAgent信息
export const ua = {
  isMobile: () => !!navigator.userAgent.match(/AppleWebKit.*Mobile.*/) || !!navigator.userAgent.match(/AppleWebKit/), //是否为移动终端
  isIOS: () => !!navigator.userAgent.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/), //ios终端
  isAndroid: () => navigator.userAgent.indexOf('Android') > -1 || navigator.userAgent.indexOf('Linux') > -1, //iphone终端
  isiPhone: () => navigator.userAgent.indexOf('iPhone') > -1 || navigator.userAgent.indexOf('Mac') > -1, //android终端
  isWindows: () => navigator.userAgent.indexOf('Windows') > -1, //widows系统
  isWebview: () => Boolean(navigator.userAgent.match(new RegExp(`(${['WebView', '; wv[)]'].join('|')})`, 'ig'))), //是否是webview
  isQQ: () => navigator.userAgent.indexOf("MQQBrowser") > -1, //内核
  isUC: () => navigator.userAgent.indexOf("UCBrowser") > -1, //内核
  isSogou: () => navigator.userAgent.indexOf("Sogou") > -1, //内核
  isBaidu: () => navigator.userAgent.indexOf("baidu") > -1, //内核
  isWeibo: () => navigator.userAgent.indexOf("WeiBo") > -1, //是否微博
  isWechat: () => navigator.userAgent.toLowerCase().indexOf("micromessenger") > -1, //是否是微信
  isAlipay: () => navigator.userAgent.toLowerCase().indexOf("alipayclient") > -1, //是否支付宝
  os: () => get_os_info(), // 获取操作系统
  browse: () => get_browse_info(), // 获取浏览器信息
  baseURL: () => get_base_url() // 当前baseurl地址
}
